var egret = window.egret;var __reflect = (this && this.__reflect) || function (p, c, t) {
    p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
};
var __extends = this && this.__extends || function __extends(t, e) { 
 function r() { 
 this.constructor = t;
}
for (var i in e) e.hasOwnProperty(i) && (t[i] = e[i]);
r.prototype = e.prototype, t.prototype = new r();
};
var Main = (function (_super) {
    __extends(Main, _super);
    function Main() {
        var _this = _super.call(this) || this;
        _this.keyList = ["score"]; //获取主域对应的分数
        _this.scrollView = new egret.ScrollView();
        _this.score = new egret.TextField(); //分数
        console.log("调取开放域成功");
        wx.onMessage(function (data) {
            console.log(data);
            if (data.isDisplay) {
                //获取小游戏开放数据接口 --- 开始
                wx.getFriendCloudStorage({
                    keyList: _this.keyList,
                    success: function (res) {
                        console.log(res);
                        _this.Data = res;
                        console.log(_this.Data);
                        _this.runGame(res, 0);
                    },
                    fail: function (err) {
                        console.log(err);
                    },
                    complete: function () {
                    }
                });
                //监听消息 isDisplay
            }
            else {
                _this.cancelGame();
            }
        });
        var imageLoader1 = new egret.ImageLoader();
        imageLoader1.addEventListener(egret.Event.COMPLETE, function (event) {
            var imageLoader1 = event.currentTarget;
            _this.panel_01 = new egret.Texture();
            _this.panel_01._setBitmapData(imageLoader1.data);
        }, _this);
        imageLoader1.load("resource/assets/pai_beijing.png"); //tanchuang_da resource/assets/panel_bg.png
        var imageLoader2 = new egret.ImageLoader();
        imageLoader2.addEventListener(egret.Event.COMPLETE, function (event) {
            var imageLoader2 = event.currentTarget;
            _this.bgtexture = new egret.Texture();
            _this.bgtexture._setBitmapData(imageLoader2.data);
        }, _this);
        imageLoader2.load("resource/assets/wenzidikuang_p.png"); //resource/assets/panel_shop_01.png paihangdikuang paihangbangdikuang1
        var imageLoader3 = new egret.ImageLoader();
        imageLoader3.addEventListener(egret.Event.COMPLETE, function (event) {
            var imageLoader3 = event.currentTarget;
            _this.biaotou = new egret.Texture();
            _this.biaotou._setBitmapData(imageLoader3.data);
        }, _this);
        imageLoader3.load("resource/assets/paihang_text.png");
        //测试点击
        _this.addEventListener(egret.TouchEvent.TOUCH_TAP, function (evt) {
            console.log('子域输出点击');
        }, _this);
        return _this;
    }
    /**
     * 便于演示数据，这里使用家数据
     * 有关获取还有的接口参考：https://mp.weixin.qq.com/debug/wxagame/dev/tutorial/open-ability/open-data.html?t=2018323
     */
    // private readonly gameData = [
    //     { openId: '', avatarUrl: '', nickName: 'peony_1', data: [{ score: 100, time: 1000 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 101, time: 100 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 102, time: 1700 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 103, time: 1800 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 104, time: 1900 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 105, time: 1070 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 106, time: 1030 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 107, time: 1010 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 108, time: 1020 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 109, time: 1030 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 111, time: 1040 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 112, time: 1050 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 123, time: 1060 }] },
    //     { openId: '', avatarUrl: '', nickName: 'peony', data: [{ score: 167, time: 1080 }] }
    // ]
    /**绘制shareCanvas */
    Main.prototype.runGame = function (data, keyIndex) {
        console.log("********" + this.numChildren);
        this.scaleX = 0.5;
        this.scaleY = 0.5;
        //排行榜背景框绘制
        var bitmap = new egret.Bitmap(this.panel_01);
        bitmap.x = 150; //179
        bitmap.y = 170; //112
        bitmap.width = 1000;
        bitmap.height = 2000;
        console.log("图片的长宽分别为：" + bitmap.x + " / " + bitmap.y);
        this.addChild(bitmap);
        //提醒
        var tip = new egret.TextField();
        this.addChild(tip);
        this.drawTitle(tip, "只显示好友前100名，每周一凌晨刷新", 240, 635, function () { });
        tip.width = 1000;
        tip.height = 1360;
        tip.$setTextColor(1);
        tip.size = 50;
        tip.textAlign = egret.HorizontalAlign.LEFT;
        tip.verticalAlign = egret.VerticalAlign.BOTTOM;
        tip.bold = true;
        //绘制排行榜每一层背景
        for (var i = 0; i < 3; i++) {
            var ceng = new egret.Bitmap(this.bgtexture);
            ceng.x = 160; //259//360
            ceng.y = i * 122 + 335; //102+233//122+233
            ceng.width = 980; //长度
            ceng.height = 100; //宽度
            this.addChild(ceng);
        }
        //滚动视图
        var listContainer = new egret.DisplayObjectContainer();
        this.scrollView.setContent(listContainer);
        this.scrollView.x = 100; //159;
        this.scrollView.y = 233; //102
        this.scrollView.width = 1300; //640
        this.scrollView.height = 400; //400
        this.addChild(this.scrollView);
        //排行榜表头绘制
        var biaotou = new egret.Bitmap(this.biaotou);
        biaotou.x = 545;
        biaotou.y = 220; //125
        this.addChild(biaotou);
        this.drawPersonData(data, keyIndex, listContainer);
    };
    /**绘制玩家信息部分 */
    Main.prototype.drawPersonData = function (data, keyIndex, listContainer) {
        var personData = this.personDataHandle(data);
        listContainer.removeChildren();
        //对接收到的数据进行降序排列
        personData.data.sort(this.sortByKey(keyIndex));
        console.log(personData.data);
        var minLength = personData.data < 8 ? personData.data.length : 8;
        console.log(minLength);
        var _loop_1 = function () {
            value = personData.data[t];
            index = t;
            if (value == undefined || value == null)
                return { value: void 0 };
            //console.log(value);
            var item = new egret.DisplayObjectContainer();
            item.y = index * 120; //行间距
            listContainer.addChild(item);
            //给玩家添加名次
            var rank = new egret.TextField();
            item.addChild(rank);
            this_1.drawTitle(rank, "排名：" + (index + 1), 0, 120, function () { }); //58
            rank.width = 300;
            rank.size = 50;
            //绘制玩家头像 
            var imageLoader1 = new egret.ImageLoader();
            imageLoader1.addEventListener(egret.Event.COMPLETE, function (event) {
                var imageLoader = event.currentTarget;
                var texture_1 = new egret.Texture();
                texture_1._setBitmapData(imageLoader.data);
                var bitmap = new egret.Bitmap(texture_1);
                bitmap.height = bitmap.width = 55; //45
                bitmap.x = 260; //170
                bitmap.y = 120;
                item.addChild(bitmap);
            }, this_1);
            imageLoader1.load(value.avatarUrl);
            //显示玩家的昵称
            var nicktxt = new egret.TextField();
            item.addChild(nicktxt);
            this_1.drawTitle(nicktxt, '名字: ' + value.nickname, 330, 120, function () { });
            nicktxt.width = 400;
            nicktxt.size = 50;
            nicktxt.textAlign = egret.HorizontalAlign.LEFT;
            //显示玩家得分
            var numtxt = new egret.TextField();
            item.addChild(numtxt);
            if (value.KVDataList[keyIndex] != undefined)
                str = value.KVDataList[keyIndex].key + ':' + value.KVDataList[keyIndex].value;
            else
                str = value.KVDataList[keyIndex].key + ':' + 100;
            this_1.drawTitle(numtxt, str.toString(), 730, 120, function () { });
            numtxt.width = 300;
            numtxt.size = 50;
            numtxt.textAlign = egret.HorizontalAlign.LEFT;
        };
        var this_1 = this, value, index, str;
        for (var t = 0; t < minLength; t++) {
            var state_1 = _loop_1();
            if (typeof state_1 === "object")
                return state_1.value;
        }
    };
    /**获取到玩家信息进行处理 */
    Main.prototype.personDataHandle = function (data) {
        var personData = data;
        for (var i = 0; i < data.length;) {
            var value = data[i];
            var index = i;
            if (data[i].KVDataList.length < 3) {
                personData.splice(i, 1);
            }
            else {
                var zeroOrUndefined = false;
                for (var j = 0; j < data[i].KVDataList.length; j++) {
                    if (data[i].KVDataList[j].value == 0 || data[i].KVDataList[j].value == undefined) {
                        zeroOrUndefined = true;
                        break;
                    }
                }
                if (zeroOrUndefined)
                    personData.splice(i, 1);
                else
                    i++;
            }
        }
        return personData;
    };
    /**绘制标题 */
    Main.prototype.drawTitle = function (score, text, x, y, fn) {
        score.text = text;
        this.setTitle(score);
        score.x = x;
        score.y = y;
        score.addEventListener(egret.TouchEvent.TOUCH_TAP, fn, this);
        score.touchEnabled = true;
    };
    /**设置标题 */
    Main.prototype.setTitle = function (text) {
        text.size = 30;
        text.textColor = text == this.score ? 0x8AFF00 : 0xFFFFFF;
        text.fontFamily = "KaiTi";
        text.width = 100;
        text.height = 50;
        text.textAlign = egret.HorizontalAlign.CENTER;
        text.verticalAlign = egret.VerticalAlign.MIDDLE;
    };
    /**远程加载资源的方法 */
    Main.prototype.reLoadImage = function (texture, path) {
        var imageLoader = new egret.ImageLoader();
        imageLoader.addEventListener(egret.Event.COMPLETE, function (event) {
            console.log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            var imageLoader_1 = event.currentTarget;
            texture = new egret.Texture();
            texture._setBitmapData(imageLoader_1.data);
        }, this);
        imageLoader.load(path);
    };
    /**根据不同的keyname 进行排序 */
    Main.prototype.sortByKey = function (keyIndex) {
        var compare = function (a, b) {
            var valueA = a.KVDataList[keyIndex].value;
            var valueB = b.KVDataList[keyIndex].value;
            if (valueA > valueB) {
                return -1;
            }
            else if (valueA < valueB) {
                return 1;
            }
            else {
                return 0;
            }
        };
        return compare;
    };
    Main.prototype.cancelGame = function () {
        for (var i = 0, l = this.numChildren; i < l; i++) {
            this.removeChildAt(0);
        }
        this.scrollView.removeContent();
        console.log('停止开放数据域');
    };
    Main.prototype.onLoadHeaderComplete = function (event) {
        var loader = event.target;
        //获取加载到的纹理对象
        var bitmapData = loader.data;
        //this.headerImg.bitmapData = bitmapData;
    };
    return Main;
}(egret.DisplayObjectContainer));
__reflect(Main.prototype, "Main");
// // 微信关系数据的获取
// // 上传方法类似、开发者自行填写
// declare namespace wx {
//     /**
//      * 监听消息
//      */
//     const onMessage: (callback: (data: { [key: string]: any }) => void) => void;
//     /**
//      * 拉取当前用户所有同玩好友的托管数据。该接口只可在开放数据域下使用
//      * @param keyList 要拉取的 key 列表
//      * @param success 接口调用成功的回调函数
//      * @param fail 	接口调用失败的回调函数
//      * @param complete 接口调用结束的回调函数（调用成功、失败都会执行）
//      */
//     const getFriendCloudStorage: (Object: {
//         keyList?: string[],
//         success?: (res: {
//             data: UserGameData[]
//         }) => void,
//         fail?: (err: any) => void,
//         complete?: () => void,
//     }) => void;
//     /**
//      * 在小游戏是通过群分享卡片打开的情况下，可以通过调用该接口获取群同玩成员的游戏数据。该接口只可在开放数据域下使用。
//      * @param shareTicket 群分享对应的 shareTicket
//      * @param keyList 要拉取的 key 列表
//      * @param success 接口调用成功的回调函数
//      * @param fail 接口调用失败的回调函数
//      * @param complete 接口调用结束的回调函数（调用成功、失败都会执行）
//      */
//     const getGroupCloudStorage: (Object: {
//         shareTicket: string,
//         keyList: string[],
//         success?: (res: {
//             data: UserGameData[]
//         }) => void,
//         fail?: (err?: any) => void,
//         complete?: () => void,
//     }) => void;
//     /**
//      * 用户数据
//      */
//     type UserGameData = {
//         /** 用户的微信头像 url */
//         avatarUrl: string,
//         /** 用户的微信昵称 */
//         nickName: string,
//         /** 用户的 openId */
//         openId: string,
//         /**用户自定义数据 */
//         KVList: KVData[]
//     }
//     type KVData = {
//         key: string,
//         value: string
//     }
// } 
;window.Main = Main;